package com.beiben.experiment.tool.service.impl;

import java.util.Date;
import java.util.List;

import com.beiben.common.GlobalConstant;
import com.beiben.experiment.equipment.domain.Equipment;
import com.beiben.experiment.repair.domain.Repair;
import com.beiben.experiment.repair.mapper.RepairMapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beiben.experiment.tool.mapper.ToolMapper;
import com.beiben.experiment.tool.domain.Tool;
import com.beiben.experiment.tool.service.IToolService;

/**
 * 工具信息Service业务层处理
 * 
 * @author 熊国强
 * @date 2020-11-11
 */
@Service
public class ToolServiceImpl implements IToolService 
{

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private ToolMapper toolMapper;

    @Autowired
    private RepairMapper repairMapper;

    /**
     * 查询工具信息
     * 
     * @param toolId 工具信息ID
     * @return 工具信息
     */
    @Override
    public Tool selectToolById(Long toolId)
    {
        return toolMapper.selectToolById(toolId);
    }

    /**
     * 查询工具信息列表
     * 
     * @param tool 工具信息
     * @return 工具信息
     */
    @Override
    public List<Tool> selectToolList(Tool tool)
    {
        return toolMapper.selectToolList(tool);
    }

    /**
     * 新增工具信息
     * 
     * @param tool 工具信息
     * @return 结果
     */
    @Override
    public int insertTool(Tool tool)
    {
        return toolMapper.insertTool(tool);
    }

    /**
     * 修改工具信息
     * 
     * @param tool 工具信息
     * @return 结果
     */
    @Override
    public int updateTool(Tool tool)
    {
        return toolMapper.updateTool(tool);
    }

    /**
     * 批量删除工具信息
     * 
     * @param toolIds 需要删除的工具信息ID
     * @return 结果
     */
    @Override
    public int deleteToolByIds(Long[] toolIds)
    {
        repairMapper.deleteRepairByRelIds(toolIds,GlobalConstant.EXPERIMENT_TOOL);
        return toolMapper.deleteToolByIds(toolIds);
    }

    /**
     * 删除工具信息信息
     * 
     * @param toolId 工具信息ID
     * @return 结果
     */
    @Override
    public int deleteToolById(Long toolId)
    {
        return toolMapper.deleteToolById(toolId);
    }

    @Override
    public String importTool(List<Tool> list, Boolean isUpdateSupport) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Tool tool : list)
        {
            try
            {
                // 验证是否存在这个用户
                Tool t = toolMapper.selectToolByCode(tool.getCode());
                if (StringUtils.isNull(t))
                {
                    this.insertTool(tool);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号： " + tool.getCode() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    tool.setToolId(t.getToolId());
                    this.updateTool(tool);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号: " + tool.getCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、编号: " + tool.getCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编号: " + tool.getCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共: " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共: " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 修改工具信息状态
     *
     * @param toolIds 工具信息IDS
     * @param state 使用状态
     * @return 结果
     */
    @Override
    public int updateToolState(Long[] toolIds, String state) {
        if("0".equals(state)){
            Repair repair = new Repair();
            repair.setRelIds(toolIds);
            repair.setTableName(GlobalConstant.EXPERIMENT_TOOL);
            repair.setRelFinishTime(new Date());
            repair.setIsFinish("Y");
            repairMapper.updateRepairIsFinish(repair);
        }
        return toolMapper.updateToolState(toolIds,state);
    }
}
